/*
  Copyright 1996-2003
  Simon Whiteside

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

* $Id: XMLExecutable.java,v 1.18 2003/03/10 19:37:28 simkin_cvs Exp $
*/
package simkin;
import java.io.*;
import org.apache.crimson.tree.*;
import org.xml.sax.*;
import org.w3c.dom.*;
import java.util.*;

/**
 * This class is a base class for objects which store their data and methods in an XML document
 *<p>Each instance is associated with an XML document which is loaded on construction. 
 */
public class XMLExecutable extends XMLElementObject {
    /**
     * This is the XML document loaded for this object
     */
    XmlDocument m_Doc;
    /**
     * Default Constructor.
     */
    public XMLExecutable() {
    }
    /**
     * Constructor taking an input stream containing an XML document
     * @param location location of the document
     * @throws SAXException where there was a syntax error in the XML stream
     * @throws IOException where there was an error loading the stream
     */
    public XMLExecutable(String location,InputStream in) throws SAXException, IOException {
	load(location,in);
    }
    /**
     * Constructor taking a reader containing an XML document
     * @param location location of the document
     * @throws SAXException where there was a syntax error in the XML stream
     * @throws IOException where there was an error loading the stream
     */
    public XMLExecutable(String location,Reader in) throws SAXException, IOException {
	load(location,in);
    }
    /**
     * This method causes the XML to be reloaded
     * @param location location of the document
     * @param in - stream containing the XML
     */
    public void load(String location,InputStream in) throws SAXException, IOException{
	m_Location=location;
	m_Doc=XmlDocument.createXmlDocument(new InputSource(in),false);
	setElement(m_Doc.getDocumentElement());
    }
    /**
     * This method causes the XML to be reloaded
     * @param location location of the document
     * @param in - reader containing the XML
     */
    public void load(String location,Reader in) throws SAXException, IOException{
	m_Location=location;
	m_Doc=XmlDocument.createXmlDocument(new InputSource(in),false);
	setElement(m_Doc.getDocumentElement());
    }
    /**
     * Call this method to save the state of the object back to a stream
     * @exception IOException if there was an error writing to the stream
     */
    public void save(OutputStream out) throws IOException{
	m_Doc.write(new OutputStreamWriter(out));
    }
    /**
     * Call this method to save the state of the object back to a writer
     * @exception IOException if there was an error writing to the writer
     */
    public void save(Writer out) throws IOException{
	m_Doc.write(out);
    }
}


